﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Serialization;

namespace RosarioFinanzas
{


    /// <summary>
    /// Clase que gestiona el RSS
    /// </summary>
    /// 
    public class RSSBLL
    {
        public RSSBLL()
        {

        }

        public static void writeRSS(RSS rss)
        {
            XmlSerializer ser = new XmlSerializer(typeof(RSS));
            StringBuilder sb = new StringBuilder();
            using (StringWriter sw = new StringWriter(sb))
            {
                using (XmlTextWriter writer = new XmlTextWriter(sw))
                {
                    writer.Formatting = Formatting.Indented;
                    ser.Serialize(sw, rss);
                }
            }

            HttpResponse response = HttpContext.Current.Response;

            response.Clear();
            response.ContentType = "text/plain";
            response.Write(sb.ToString());
            response.Flush();
            response.End();

        }

        public static RSS readRSS(string url)
        {
            RSS rss = new RSS();

            XmlSerializer ser = new XmlSerializer(typeof(RSS));

            using (XmlTextReader reader = new XmlTextReader(url))
            {
                try
                {
                    rss = ser.Deserialize(reader) as RSS;
                }
                catch (Exception e)
                {
                    HttpContext.Current.Response.Write(e.Message);
                }
            }

            return rss;
        }
    }
}

